﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.home
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FrdSearch : IHttpHandler
    {
        private int userID;//用户ID
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);


            string msg =string.Empty;
            string searchEmail = context.Request["txtEmail"];
            //string strBeAccessUser = context.Request["UserID"];
            if (!string.IsNullOrEmpty(searchEmail))
            {
                //Jy123.Model.User_Request mRequest = new Jy123.Model.User_Request();
                //Jy123.BLL.User_Request bllRequest = new Jy123.BLL.User_Request();
                Jy123.Model.User_Friend mFriend = new Jy123.Model.User_Friend();
                Jy123.BLL.User_Friend bllFriend = new Jy123.BLL.User_Friend();

                mFriend.FUser = userID;
                int userReceiverID = 0;
                userReceiverID = new BLL.User.Friend().SearchUserIDbyEmail(searchEmail);
                if (userReceiverID !=0)
                {
                    int isRequested = new BLL.User.Friend().AlreadySearch(userID, userReceiverID);
                    switch (isRequested)
                    {
                        case 0:
                            msg = "已发送过请求!,warning"; break;
                        case 1:
                            msg = "已在您的好友列表中!,warning"; break;
                        case 2:
                            mFriend.FUser = userID;
                            mFriend.FFriend = userReceiverID;
                            mFriend.State = 0;//等待确认
                            mFriend.AddTime = DateTime.Now;
                            bllFriend.Add(mFriend);
                            msg = "请求发送成功!,ok";
                            break;
                    }                    
                }
                else
                {
                    msg = "用户不存在!,error";
                }
                
            }
            else
            {
                msg = "输入不可为空!,error";
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
